package ssm.ann;

import org.h2.server.web.WebServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import ssm.ann.config.RootConfig;
import ssm.ann.config.ServletConfig;

import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // spring 配置
        return new Class<?>[]{RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // spring mvc 配置
        return new Class<?>[]{ServletConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        // DispatcherServlet 拦截路径，/* 拦截所有，/ 会排除 *.jsp 文件
        return new String[]{"/"};
    }

    @Override
    protected void registerContextLoaderListener(ServletContext servletContext) {
        super.registerContextLoaderListener(servletContext);
        // servletContext.addListener(new DbStarter());
        ServletRegistration.Dynamic h2 = servletContext.addServlet("h2", new WebServlet());
        h2.addMapping("/console/*");
    }
}